package com.wq.order.service;

import com.wq.api.domain.entity.order.Order;
import com.wq.api.domain.entity.order.Product;
import com.wq.api.repository.order.OrderRepository;
import com.wq.api.repository.order.ProductRepository;
import com.wq.api.service.order.OrderService;
import com.wq.order.factory.OrderFactory;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by changming.xie on 3/25/16.
 */
@Service
@DubboService
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderRepository orderRepository;

    @Autowired
    OrderFactory orderFactory;

    @Autowired
    ProductRepository productRepository;

    @Override
    public Order createOrder(long payerUserId, long payeeUserId, List<Pair<Long, Integer>> productQuantities) {
        Order order = orderFactory.buildOrder(payerUserId, payeeUserId, productQuantities);

        orderRepository.createOrder(order);

        return order;
    }

    @Override
    public Order findOrderByMerchantOrderNo(String orderNo) {
        return orderRepository.findByMerchantOrderNo(orderNo);
    }

    @Override
    public List<Product> findByShopId(long shopId) {
        return productRepository.findByShopId(shopId);
    }

    @Override
    public Product findById(long productId) {
        return productRepository.findById(productId);
    }
}
